package com.health.db.dao.user.impl;
 
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.user.ICourseShareJournalDao;
import com.health.db.mapper.CourseShareJournalMapper;
import com.health.db.model.CourseShareJournal;
import com.health.db.model.CourseShareJournalExample;
import com.health.db.model.UserAccountJournalTemp;
import com.health.redis.RedisConstants;

@Component("courseShareJournalDao")
public class CourseShareJournalDaoImpl extends ServiceInterface implements ICourseShareJournalDao {

	@Autowired
	private  CourseShareJournalMapper courseShareJournalMapper;
	String  userId=":userId:";
	String  offset=":OF:";
	String  pageSize=":PS:";

	public CourseShareJournalMapper getCourseShareJournalMapper() {
		return courseShareJournalMapper;
	}

	public void setCourseShareJournalMapper(
			CourseShareJournalMapper courseShareJournalMapper) {
		this.courseShareJournalMapper = courseShareJournalMapper;
	}

	@Override
	public int insert(CourseShareJournal record) {
		// TODO Auto-generated method stub
		
		int result=courseShareJournalMapper.insert(record);
		getRedisOperatiorLogic();
		String key = RedisConstants.CourseShareJournalMapper+userId+record.getAccountid().longValue();
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteKeys(key,  getOrderRedisGroupNo("1"));
		}
		return result;
	}
	
	@Override
	public int delete(CourseShareJournal record) {
		// TODO Auto-generated method stub
		int result=courseShareJournalMapper.deleteByPrimaryKey(record.getRecid());
		getRedisOperatiorLogic();
		String key = RedisConstants.CourseShareJournalMapper+userId+record.getAccountid().longValue();
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteKeys(key,  getOrderRedisGroupNo("1"));
		}
		return result;
	}
	
	
	

	@Override
	public List<CourseShareJournal> selectByExample(
			CourseShareJournalExample example) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateByPrimaryKeySelective(CourseShareJournal record) {
		// TODO Auto-generated method stub
		
		int result=courseShareJournalMapper.updateByPrimaryKeySelective(record);
		getRedisOperatiorLogic();
		String key = RedisConstants.CourseShareJournalMapper+userId+record.getAccountid().longValue();
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteKeys(key,  getOrderRedisGroupNo("1"));
		}
		return result;
	}

	@Override
	public List<CourseShareJournal> selectByUserId(Long userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UserAccountJournalTemp> selectCourseShareJournalSimple(
			CourseShareJournalExample example) {
		// TODO Auto-generated method stub
		
		getRedisOperatiorLogic();
		String key=RedisConstants.CourseShareJournalMapper+userId+example.getUserId().longValue()
				+offset+example.getOffset()+pageSize+example.getPageSize();
		String keyList = RedisConstants.CourseShareJournalMapper+userId+example.getUserId().longValue();
		
		Object object=null;
		List<UserAccountJournalTemp> userAccountJournalTemp=null;
//com.health.db.model.UserAccountJournalTemp
		if(redisOperatiorLogic.isJedisIsConnected())
		{	
			//get data from redis
			object = redisOperatiorLogic.readObject(key, getPublicRedisGroupNo("1"));
		 }
		
	     if(null!=object){
	    	if (!object.equals("")) {
	    		 userAccountJournalTemp =  (List<UserAccountJournalTemp>) object;
			}
	     }
	     else{
	         userAccountJournalTemp =courseShareJournalMapper.selectCourseShareJournalSimple(example);
		    if(redisOperatiorLogic.isJedisIsConnected())
			{
				redisOperatiorLogic.setEx(key, userAccountJournalTemp,  getOrderRedisGroupNo("1"));
				redisOperatiorLogic.lpush(keyList, key, getOrderRedisGroupNo("1"));
			}
		 }		
		return userAccountJournalTemp;
	}


}
